/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package StoreType;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 *
 * @author aamod
 */
public class Query {
    static Connection con = null;
    static ResultSet rs = null;
    public static int add(StoreTypeBean sb) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("insert into store_type values(storetypeid.nextval,?,sysdate)");
            pre.setString(1, sb.getStoreType());
            i = pre.executeUpdate();
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }
    
    public static Vector viewAll() throws SQLException {
        int i = 0;
        Vector<StoreTypeBean> list=new Vector<StoreTypeBean>();
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select * from store_type");
            rs = pre.executeQuery();
            while (rs.next()) {
                StoreTypeBean sb = new StoreTypeBean();
                sb.setStoreTypeId(rs.getInt(1));
                sb.setStoreType(rs.getString(2));
                sb.setEntryDate(rs.getDate(3));
                list.add(sb);
            }
        } catch (Exception e) {
            System.out.println(e);
            return list;
        } finally {
            con.close();
        }
        return list;
    }
    
    public static int delete(int id) throws SQLException
        {
            int i=0;
            try
            {
              con=DBConnection.Connection.getConnection();
              PreparedStatement pre=con.prepareStatement("delete from store_type where store_type_id=?");
              pre.setInt(1,id);
              i=pre.executeUpdate();
            }
            catch(Exception e)
            {
               return i; 
            }
            finally
            {
              con.close();  
            }
            return i;
        }
    public static int edit(StoreTypeBean sb) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("update store_type set store_type=?,ENTRY_DATE=sysdate where store_type_id=?");
            pre.setString(1, sb.getStoreType());
            pre.setInt(2, sb.getStoreTypeId());
            i = pre.executeUpdate();
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }
}
